﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace AshuSoft.Test
{
    internal partial class DataSetViewerForm : Form
    {
        private static int InstanceCount;
        private const string TitleFormat = "AshuSoft Data Viewer - DataSet: {0}, Table: {1}";
        private readonly DataSet _dataSet;
        private readonly string _dataSetFileName;
        public DataSetViewerForm(DataSet dataSet, string callStack)
        {
            _dataSet = dataSet;
            InitializeComponent();
            callStackTextBox.Text = callStack;
        }
        public DataSetViewerForm(DataSet dataSet, string dataSetFile, string callStack)
        {
            if(!File.Exists(dataSetFile))
            {
                throw new FileNotFoundException(String.Format("File {0} doesn't exits", dataSetFile));
            }
            
            InitializeComponent();
            _dataSet = dataSet;
            _dataSetFileName = dataSetFile;
            saveChangesButton.Visible = true;
            
            callStackTextBox.Text = callStack;
        }

        void ShowTableList()
        {
            List<string> selectedTables;
            IList<DataTable> dataTables = new List<DataTable>(_dataSet.Tables.OfType<DataTable>());
            if(checkBox1.Checked)
            {
                selectedTables = dataTables.Where(p => p.Rows.Count > 0).Select(p => p.TableName).ToList();
            }
            else
            {
                selectedTables = dataTables.Select(p => p.TableName).ToList();
            }
            selectedTables.Sort();
            listBox1.Items.Clear();
            listBox1.Items.AddRange(selectedTables.ToArray());
            dataGridView1.DataSource = null;
            Text = String.Format(TitleFormat, _dataSet.DataSetName, "");
        }

        private void ListBox1SelectedIndexChanged(object sender, EventArgs e)
        {
            if(listBox1.SelectedItem != null)
            {
                DataTable table = _dataSet.Tables[(string)listBox1.SelectedItem];
                dataGridView1.DataSource = table;
                Text = String.Format(TitleFormat, _dataSet.DataSetName, table.TableName);
            }
        }

        private void Button1Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                DataTable table = _dataSet.Tables[(string) listBox1.SelectedItem];
                TableDataViewer.ShowTable(table, table.TableName);
                Text = String.Format(TitleFormat, _dataSet.DataSetName, table.TableName);
            }
        }

        private void CheckBox1Click(object sender, EventArgs e)
        {
            ShowTableList();
        }

        private void DataSetViewerFormLoad(object sender, EventArgs e)
        {
            ShowTableList();
            label2.Text = String.Format("ID = {0}, Launch Time = {1}", ++InstanceCount, DateTime.Now.ToShortTimeString());
        }

        private void saveChangesButton_Click(object sender, EventArgs e)
        {
            try
            {
                _dataSet.WriteXml(_dataSetFileName, XmlWriteMode.WriteSchema);
                MessageBox.Show("DataSet changes saved successfully", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to save DataSet changes " + ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
        private void saveAsButton_Click(object sender, EventArgs e)
        {
            SaveFileDialog openFileDialog = new SaveFileDialog();
            openFileDialog.Filter = "Data Set File (*.xml)|*.xml";
            if(openFileDialog.ShowDialog() ==DialogResult.OK)
            {
                try
                {
                    _dataSet.WriteXml(openFileDialog.FileName, XmlWriteMode.WriteSchema);
                    MessageBox.Show("DataSet changes saved successfully", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Failed to save DataSet changes " + ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        
    }
}
